package com.zk.babala.module;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.zk.babala.R;
import com.zk.babala.entity.PlayNumInfo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 视频播放选集控制
 */
public class EpisodeControlBar extends LinearLayout {
    private LinearLayout episodeLayout;
    private OnEpisodeSelectedListener listener;
    List<PlayNumInfo> allEpisodes;
    int currId = 0;
    public EpisodeControlBar(Context context) {
        super(context);
        init();
    }

    public EpisodeControlBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public EpisodeControlBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // 设置方向
        setOrientation(LinearLayout.VERTICAL);

        // 加载布局
        LayoutInflater.from(getContext()).inflate(R.layout.episode_control_bar, this, true);

        // 初始化控件
        episodeLayout = findViewById(R.id.episode_list_layout);
    }

    // 设置选集数据
    public void setEpisodes(PlayNumInfo currPlayNumInfo) {
        if (allEpisodes.isEmpty()) {
            return;
        }
        currId = currPlayNumInfo.getId();
        episodeLayout.removeAllViews();
        List<PlayNumInfo> episodes = allEpisodes.stream().filter(playNumInfo -> playNumInfo.getId() > currPlayNumInfo.getId() - 2).collect(Collectors.toList());
        for (int i = 0; i < episodes.size(); i++) {
            PlayNumInfo playNumInfo = episodes.get(i);
            Button episodeButton = new Button(getContext());
            episodeButton.setText(playNumInfo.getTitle());
            episodeButton.setTextSize(12);
            // 设置焦点相关属性
            // 设置按钮点击事件
            episodeButton.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onEpisodeSelected(playNumInfo);
                }
            });
            episodeButton.setTextColor(Color.WHITE);
            episodeButton.setBackgroundResource(R.drawable.style_button);

            // 设置按钮布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            params.setMargins(4, 0, 4, 0);
            episodeButton.setLayoutParams(params);
            episodeLayout.addView(episodeButton);
        }
    }

    // 设置选集选择监听器
    public void setOnEpisodeSelectedListener(OnEpisodeSelectedListener listener) {
        this.listener = listener;
    }

    // 选集选择监听器接口
    public interface OnEpisodeSelectedListener {
        void onEpisodeSelected(PlayNumInfo playNumInfo);
    }

    // 显示控制条
    public void show() {
        setVisibility(View.VISIBLE);
        episodeLayout.bringToFront();
        //默认选中下一集
        int nextId = 2;
        //第一集
        if (currId == 0) {
            nextId = 1;
        }
        // 最后一集
        if (episodeLayout.getChildCount() == 2) {
            nextId = 0;
        }
        View firstChild = episodeLayout.getChildAt(nextId);
        if (firstChild != null) {
            firstChild.requestFocus();
        }
    }

    // 隐藏控制条
    public void hide() {
        setVisibility(View.GONE);
    }

    public void setAllEpisodes(List<PlayNumInfo> allEpisodes) {
        this.allEpisodes = allEpisodes;
    }
}
